
#include "imageViewer.h"

#include <QtGui/QPainter>
#include <QtGui/QPalette>
#include <QtGui/QColor>
#include <QtGui/QPen>
#include <QtGui/QBrush>
#include <QtCore/QPoint>
#include <QtCore/Qt>

#include <QtCore/QString>

/*----------------------------------------------------------------------------*/

ImageViewer::ImageViewer( QWidget* _parent )
	:	QWidget( _parent )
	,	m_imageAvailable( false )
	,	m_showLinesFrames( true )
	,	m_showWordsFrames( true )
	,	m_showSymbolsFrames( true )
	,	m_image()
	,	m_linesFrames()
	,	m_wordsFrames()
	,	m_symbolFrames()	
{	
	QPalette palette;
	palette.setBrush( this->backgroundRole(), QBrush( QColor( Qt::white ) ) );
	this->setPalette( palette );
	this->setAutoFillBackground( true );	
}


/*----------------------------------------------------------------------------*/


ImageViewer::~ImageViewer()
{
	cleanUp();
}


/*----------------------------------------------------------------------------*/


void ImageViewer::paintEvent( QPaintEvent* )
{		
	QPainter painter;	

	/** create solid pen */
	QPen solidPen = Qt::SolidLine;
	solidPen.setWidth( 1 );	

	/** draw detected frames once needed */
	if( m_imageAvailable )
	{
		/** copy main image */
		QImage paintImage( m_image );

		/** draw into main image */
		painter.begin( &paintImage );
		painter.initFrom( this );
		painter.setRenderHint( QPainter::Antialiasing, true );		

		/** draw lines frames */
		if ( m_showLinesFrames )
		{
			solidPen.setColor( Qt::red );
			painter.setPen( solidPen );

			for ( size_t i = 0; i < m_linesFrames.size(); i++ )			
				painter.drawRect( QRect( m_linesFrames[i].topLeft(), m_linesFrames[i].bottomRight() ) );			
		}

		/** draw words frames */
		if ( m_showWordsFrames )
		{			
			solidPen.setColor( Qt::blue );
			painter.setPen( solidPen );

			for( size_t i = 0; i < m_wordsFrames.size(); i++ )			
				painter.drawRect( QRect( m_wordsFrames[i].topLeft(), m_wordsFrames[i].bottomRight() ) );			
		}

		/** draw symbols frames */
		if ( m_showSymbolsFrames )
		{
			solidPen.setColor( Qt::magenta );
			painter.setPen( solidPen );

			for ( size_t i = 0; i < m_symbolFrames.size(); i++ )							
				painter.drawRect( QRect( m_symbolFrames[i].topLeft(), m_symbolFrames[i].bottomRight() ) );
		}

		painter.end();
		
		/** create scaled image to display */
		QImage scaledImage = paintImage.scaled( this->width() - 10, this->height() - 10,
												Qt::KeepAspectRatio, Qt::SmoothTransformation );		

		/** count display offset */
		QPoint offset = this->frameGeometry().center() - scaledImage.rect().center() - QPoint( 10, 1 );

		/** paint scaled image */
		painter.begin( this );		
		painter.setRenderHint( QPainter::TextAntialiasing, true );	
		painter.drawImage( offset, scaledImage );
		painter.end();
	}

	/** drawing widget frame */	
	painter.begin( this );	
	solidPen.setColor( Qt::black );
	painter.setPen( solidPen );
	QRect widgetFrame( this->rect() );
	widgetFrame.setBottomRight( widgetFrame.bottomRight() - QPoint( 1, 1 ) );	
	painter.drawRect( widgetFrame );
	painter.end();	
}


/*----------------------------------------------------------------------------*/


void ImageViewer::SetImage( QImage& _image )
{
	cleanUp();
	m_image = _image;
	m_imageAvailable = true;
	emit ImageAccessible( true );
	this->repaint();
}	


/*----------------------------------------------------------------------------*/


void ImageViewer::CloseImage()
{
	cleanUp();	
	m_imageAvailable = false;
	emit ImageAccessible( false );
	this->repaint();
}


/*----------------------------------------------------------------------------*/


void ImageViewer::cleanUp()
{		
	m_symbolFrames.clear();
	m_wordsFrames.clear();
	m_linesFrames.clear();
}


/*----------------------------------------------------------------------------*/


void ImageViewer::SetLinesFrames( QRectV& _linesFrames )
{
	m_linesFrames.clear();
	m_linesFrames = _linesFrames;
	this->repaint();
}


/*----------------------------------------------------------------------------*/


void ImageViewer::SetWordFrames( QRectV& _wordsFrames )
{
	m_wordsFrames.clear();
	m_wordsFrames = _wordsFrames;
	this->repaint();
}


/*----------------------------------------------------------------------------*/


void ImageViewer::SetSymbolFrames( QRectV& _symbolFrames )
{
	m_symbolFrames.clear();
	m_symbolFrames = _symbolFrames;
	this->repaint();
}


/*----------------------------------------------------------------------------*/


void ImageViewer::SetFramesShowing( bool _lines, bool _words, bool _symbols )
{
	m_showLinesFrames   = _lines;
	m_showWordsFrames   = _words;
	m_showSymbolsFrames = _symbols;
	this->repaint();
}


/*----------------------------------------------------------------------------*/

